//
//  HGOderDetailVC.m
//  RedWinMall
//
//  Created by Henry on 2018/8/6.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGOderDetailVC.h"
#import "HGOrderCell.h"
#import "HGGoodsOderCell.h"
#import "HGAddressModel.h"
#import "HGGoodsModel.h"
#import "HGOderInfoCell.h"

@interface HGOderDetailHeadeView : UIView

@property (copy, nonatomic) NSString *type;

@end

@implementation HGOderDetailHeadeView {
    UILabel *_typeLabel;//新增
}

- (instancetype)init {
    if (self = [super init]) {
        [self initSubViews];
    }
    return self;
}

- (void)setType:(NSString *)type {
    _typeLabel.text = [NSString stringWithFormat:@"%@\n剩11时48分 自动取消",type];
    [Utility fuwenbenLabel:_typeLabel fontNumber:[UIFont systemFontOfSize:20] andRange:NSMakeRange(0, type.length) andColor:[UIColor whiteColor]];
}

- (void)initSubViews {
    
    self.backgroundColor = kColorMainTheme;
    
    _typeLabel = [[UILabel alloc] init];
    _typeLabel.text = @"等待买家付款\n剩11时48分 自动取消";
    _typeLabel.textColor = [UIColor whiteColor];
    _typeLabel.numberOfLines = 2;
    _typeLabel.font = [UIFont systemFontOfSize:11];
    [self addSubview:_typeLabel];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).multipliedBy(0.5);
        make.top.offset(kNavigationHeight + 15);
    }];
    
    
}

@end

@interface HGOderDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong)UITableView* tableView;

@property (nonatomic,strong)HGOderDetailHeadeView* headView;

@property (nonatomic,strong)HGAddressModel* addressModel;

@property (nonatomic,strong)HGGoodsModel* infoModel;

@end

@implementation HGOderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSubViews];
    
    [self initDatas];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{ NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{ NSForegroundColorAttributeName:[UIColor blackColor]}];
}


- (void)initSubViews {
    
    self.title = @"订单详情";
    
    [self.tableView registerClass:[HGOrderAddressCell class] forCellReuseIdentifier:@"HGOrderAddressCell"];
    
    [self.tableView registerClass:[HGGoodsOderCell class] forCellReuseIdentifier:@"HGGoodsOderCell"];
    
    [self.tableView registerClass:[HGOderInfoCell class] forCellReuseIdentifier:@"HGOderInfoCell"];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        HGOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGOrderAddressCell"];
        cell.model = _addressModel;
        cell.type = 1;
        return cell;
    } else if (indexPath.row == 1) {
        HGGoodsOderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGGoodsOderCell"];
        cell.model = _goodsModel;
        cell.type = 1;
        return cell;
    } else {
        HGOderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGOderInfoCell"];
        cell.model = _infoModel;
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (void)initDatas {
    
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/order/get") parameters:@{@"id" : [NSNumber numberWithLong:_goodsModel.goodsId]} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            _infoModel = [HGGoodsModel mj_objectWithKeyValues:response.responseModel.data];
            _addressModel = [HGAddressModel mj_objectWithKeyValues:response.responseModel.data];
            NSString *text = @"";
            if (_infoModel.sendState == 1 && _infoModel.state == 2) {
                text = @"等待卖家发货";
            } else if (_infoModel.sendState == 2 &&_infoModel.state == 2) {
                text = @"卖家已发货";
            } else {
                text = _infoModel.stateText;
            }
            _headView.type = text;
            [_tableView reloadData];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 62;
    } else {
        return indexPath.row == 1 ? 86 : 220;
    }
}


- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headView;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"UITableViewHeaderFooterView"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 40;
        [self.view addSubview:_tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (HGOderDetailHeadeView *)headView {
    if (!_headView) {
        _headView = [[HGOderDetailHeadeView alloc] init];
        _headView.height = 135 * kScalingH;
    }
    return _headView;
}

- (BOOL)shouldShowShadowImage {
    return NO;
}

- (UIImage *)navigationBarBackgroundImage {
    return [UIImage new];
}

- (BOOL)navigationIsLight {
    return YES;
}

@end
